from SystematicFactors.General import *
import os
import Core.Gadget as Gadget
import Core.MySQLDB as MySQLDB

# 新旧印子映射表


def Statistics_Factors(database):
    factors = database.Find("Factor", "a_sys_factor_map", {"strategy_category":"CHNBOND"})
    for factor_document in factors:
        factor_name = factor_document["factor_name"]
        documents = database.Find("Factor", "a_sys_factor", filter={"name": factor_name}, sort=[("Date",1)])
        if len(documents) == 0:
            print(factor_name, "No Data")
        else:
            print(documents[0]["date"], documents[-1]["date"], "#", len(documents), factor_name)


def Insert_Map(database, factor_map):
    factor_list = []
    for f in factor_map:
        doc = {}
        doc["former_name"] = f[0]
        doc["factor_name"] = f[1]
        doc["strategy_category"] = f[2]
        doc["key2"] = f[0] + "_" + f[1] + "_" + f[2]
        factor_list.append(doc)
    #
    database.Upsert_Many("factor","a_sys_factor_map", {}, factor_list)


def Insert_Map_HS300(database, path_file_name):
    factor_map = []
    # 新因子 20200807
    factor_map.append(["", "Est_EPS_HS300_Weekly_Chg", "HS300"])
    factor_map.append(["", "Est_EPS_HS300_Momentum_1M", "HS300"])
    factor_map.append(["", "Est_EPS_HS300_Momentum_3M", "HS300"])
    factor_map.append(["", "Est_EPS_HS300_Momentum_6M", "HS300"])

    df = pd.read_excel(path_file_name, sheet_name="沪深300映射", encoding='gbk', header=0)
    #
    for key, row in df.iterrows():
        former_name = row["Former Name"]
        factor_name = row["New Name"]
        factor_map.append([former_name, factor_name, "HS300"])
    #
    for element in factor_map:
        print(element)

    Insert_Map(database, factor_map)


def Insert_Map_SP500(database, path_file_name):
    factor_map = []
    # CHNBOND
    # factor_map.append(["H11001", "H11001_Monthly_Return", "SPX"])
    # factor_map.append(["OMO", "CB_NetInvested_Monthly", "SPX"])
    # factor_map.append(["SLFMLFPSL", "SLF_MLF_PSL_Monthly_Dif", "SPX"])
    # factor_map.append(["GOVT_leverage", "Govt_Leverage_Monthly_Dif", "SPX"])
    # factor_map.append(["DIFLN_FXreserve", "FX_Reserve_Monthly_Dif", "SPX"])
    #
    df = pd.read_excel(path_file_name, sheet_name="SP500映射", encoding='gbk', header=0)
    #
    for key, row in df.iterrows():
        former_name = row["Former Name"]
        factor_name = row["New Name"]
        factor_map.append([former_name, factor_name, "SPX"])
    #
    # print(df)
    for element in factor_map:
        print(element)

    Insert_Map(database, factor_map)


def Insert_Map_CHNBOND(database, path_file_name):
    factor_map = []
    # CHNBOND
    #
    df = pd.read_excel(path_file_name, sheet_name="中债映射", encoding='gbk', header=0)
    #
    for key, row in df.iterrows():
        former_name = row["Former Name"]
        factor_name = row["New Name"]
        factor_map.append([former_name, factor_name, "CHNBOND"])
    #
    for element in factor_map:
        print(element)

    Insert_Map(database, factor_map)


if __name__ == '__main__':
    #
    pathfilename = os.getcwd() + "\..\Config\config2.json"
    config = Config.Config(pathfilename)
    database = config.DataBase("JDMySQL")
    realtime = config.RealTime()

    path_file_name = "C:/Users/fengshimeng3/Documents/TimingModel/系统性因子3-20200810.xlsx"

    Insert_Map_HS300(database, path_file_name)
    Insert_Map_SP500(database, path_file_name)
    Insert_Map_CHNBOND(database, path_file_name)





